'use strict';
/* Controllers */
app.controller('nomemberCtrl', ['$scope', 'apiService', 'toaster', '$rootScope', 'ngDialog', '$timeout', function ($scope, apiService, toaster, $rootScope, ngDialog, $timeout) {
    $scope.search_obj = {
        memberId: $rootScope.userObj['id'],
        name: '',
        phone: '',
        email: '',
        idcard: '',
        pageSize: 10,
        role: 1 //非会员
    };

    $scope.paginationConf = {
        currentPage: 1,
        totalItems: 66,
        itemsPerPage: $scope.search_obj['pageSize'],
        perPageOptions: [10, 20, 30, 40, 50],
        onChange: function () {
            $scope.search_obj['rows'] = $scope.paginationConf.currentPage;
            // 获取非会员列表
            apiService.post({
                modelName: 'userinfo',
                apiName: 'getUserinfoByLike'
            }, $scope.search_obj, function (data) {
                if (data.code == 200) {
                    $scope.pageData = data['data']['list'];
                    $scope.paginationConf.totalItems = data['data']['total']? data['data']['total']:0;
                } else {
                    toaster.pop('error', '提示', data['msc']);
                }
            }, function (err) {
                toaster.pop('error', '提示', '服务器出小差咯...');
            });
        }
    };


    // $scope.$watch('search_obj',function(){
    //     $scope.paginationConf.onChange();
    // },true);

    $scope.searchFun = function () {
        $scope.paginationConf.onChange();
    }

    $scope.tranlateMember = function (item) {
        item['userinfoId'] = item['id'];
        item['previous'] = $rootScope.userObj['id'];
        // 对话框
        $scope.dialog = {
            title: '提示',
            content: '真的要转换吗?<br/>转化后将从您的账户扣除980元资金！<br/>请输入交易密码:',
            level:0,
            previous:$rootScope.userObj['previous'],
            username:item['name']
        };
        ngDialog.openConfirm({
            template: '/tpl/dialog_nomember.html',
            className: 'ngdialog-theme-plain',
            scope: $scope
        }).then(function (value) {
            if (value['statu'] == true) {
                item.paypwd=value.val;
                item.level=value.level;
                item.username=value.username;
                if(!item.company){
                    item.company=2;
                }

                apiService.post({
                    modelName: 'member',
                    apiName: 'ZhuanMemDB'
                }, item, function (data) {
                    if (data.code == 200) {
                        toaster.pop('success', '提示', data['msc']);
                        // 对话框
                        $scope.dialog = {
                            title: '提示',
                            content: '注册新会员成功，已扣除相应的金额!<br /> 上级会员名:' + data['countdb']['username'] + '<br /> 新创建的会员信息:<br/ > 用户名:' + data['memberdb']['username'] + '<br />密码:123456(默认)'
                        };
                        ngDialog.openConfirm({
                            template: '/tpl/dialog.html',
                            className: 'ngdialog-theme-plain',
                            scope: $scope,
                            preCloseCallback: function () {
                                window.location.reload();
                            }
                        }).then(function (value) {
                            window.location.reload();
                        });
                    } else {
                        toaster.pop('error', '提示', data['msc']);
                    }
                }, function (err) {
                    toaster.pop('error', '提示', '服务器出小差咯...');
                });
            }
        });

    }

}]);